package com.programs.waw;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AccesPointAdapter extends ArrayAdapter<AccesPoint> {

	private final int resourceId;
	private final LayoutInflater inflater;

	public AccesPointAdapter(Context context, int resourceId,
			List<AccesPoint> objects) {

		super(context, resourceId, objects);

		this.resourceId = resourceId;
		inflater = LayoutInflater.from(context);

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		AccesPoint a = getItem(position);

		AccesPointViewCache viewCache;

		if (convertView == null) {
			convertView = inflater.inflate(resourceId, null);
			viewCache = new AccesPointViewCache(convertView);
			convertView.setTag(viewCache);
		} else {
			viewCache = (AccesPointViewCache) convertView.getTag();
		}

		ImageView signalRes = viewCache.getImageSignal();
		int signalLevel = a.getSignalLevel();

		if (signalLevel > -100 && signalLevel <= -81) {
			signalRes.setImageResource(R.drawable.signal_25);
		} else if (signalLevel > -80 && signalLevel <= -61) {
			signalRes.setImageResource(R.drawable.signal_50);
		} else if (signalLevel > -60 && signalLevel <= -46) {
			signalRes.setImageResource(R.drawable.signal_75);
		} else {
			signalRes.setImageResource(R.drawable.signal_100);
		}

		TextView ssid = viewCache.getTextViewSsid();
		ssid.setText(a.getSsid());

		TextView bssid = viewCache.getTextViewBssid();
		bssid.setText(a.getCapabilities());

		String s = a.getSsid();

		if (s.startsWith("Alice-") || s.startsWith("Telecom-")
				|| s.startsWith("FASTWEB-1-001CA2")
				|| s.startsWith("FASTWEB-1-001DBX")
				|| s.startsWith("FASTWEB-1-00036F")
				|| s.startsWith("FASTWEB-1-002196")
				|| s.startsWith("InfostradaWiFi-")) {

			ssid.setTextColor(Color.parseColor("#ffffff"));
			bssid.setTextColor(Color.parseColor("#6b71f1"));

		}

		return convertView;

	}
}
